

/*
* Copyright 2005-2009 WSO2, Inc. http://wso2.com
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
 
 
#include <stdio.h>
#include <ServiceClient.h>
#include <OMElement.h>
#include <iostream>
//#include <AxisFault.h>
#include <WSFault.h>
#include <Environment.h>

using namespace std;
using namespace wso2wsf;

OMElement* createChildElement(OMElement*& pParent, char* pName, char* pText) {
   char log_msg[1024];
   sprintf(log_msg, "child element name: %s, child element text: %s", pName, pText);

   OMElement* pChildElement = new OMElement(pName);
   pChildElement->setText(pText);
   bool bRetVal = pParent->addChild(pChildElement);

   printf("returning from createChildElement\n");
   return pParent;
}

OMElement* assembleUserRegistrationPkt() {
	OMElement * payload = new OMElement(NULL, "registeruser");
	createChildElement(payload, "source", "testapp");
	OMElement* pDetailsEle = new OMElement(payload, "details");
	createChildElement(pDetailsEle, "emailid", "nandan@gmail.com");
	createChildElement(pDetailsEle, "password", "hello");
	createChildElement(pDetailsEle, "name", "paddy");
	createChildElement(pDetailsEle, "address", "1260, noida");
	createChildElement(pDetailsEle, "city", "noida");
	createChildElement(pDetailsEle, "state", "UP");
	createChildElement(pDetailsEle, "country", "India");
	createChildElement(pDetailsEle, "zip", "110011");
	createChildElement(pDetailsEle, "phone", "01143018553");
	createChildElement(pDetailsEle, "dob", "1981-12-12");
	createChildElement(pDetailsEle, "imid", "kam1al.nandan");
	createChildElement(pDetailsEle, "imservice", "gmail");

	return payload;
}

//<confirmuserregistration>
//   <source>web-browser</source>
//   <details>
//      <emailid>kamal.nandan@gmail.com</emailid>
//      <userid>kamal.nandan</userid>
//   </details>
//</confirmuserregistration>
 
OMElement* assembleUserRegistrationConfirmationPkt() {
	OMElement * payload = new OMElement(NULL, "confirmuserregistration");
	createChildElement(payload, "source", "testapp");
	OMElement* pDetailsEle = new OMElement(payload, "details");
	createChildElement(pDetailsEle, "emailid", "nandan@gmail.com");

	return payload;
}

OMElement* assembleUserLoginPkt() {
	OMElement * payload = new OMElement(NULL, "login");
	createChildElement(payload, "source", "testapp");
	OMElement* pDetailsEle = new OMElement(payload, "details");
	createChildElement(pDetailsEle, "emailid", "hello.kr.dev1@gmail.com");
	createChildElement(pDetailsEle, "password", "hello");

	return payload;
}


OMElement* assembleUserLogoutPkt() {
	OMElement * payload = new OMElement(NULL, "logout");
	createChildElement(payload, "source", "testapp");
	OMElement* pDetailsEle = new OMElement(payload, "details");
	createChildElement(pDetailsEle, "emailid", "hello.kr.dev1@gmail.com");
	createChildElement(pDetailsEle, "userid", "19");
	createChildElement(pDetailsEle, "tokenid", "78ca0bff-45fd-4b6a-9cc6-035b30ac3b5e");
	return payload;
}


OMElement* assembleAddInfoPkt() {
	OMElement * payload = new OMElement(NULL, "addinfo");
	createChildElement(payload, "source", "testapp");
	createChildElement(payload, "emailid", "hello.kr.dev1@gmail.com");
	createChildElement(payload, "tokenid", "abc3224f-fd0f-4cfc-90de-98c0e609f7bc");
	OMElement* pDetailsEle = new OMElement(payload, "details");
	createChildElement(pDetailsEle, "info", "lent money to Amit");
	createChildElement(pDetailsEle, "media", "0");
	createChildElement(pDetailsEle, "expirydate", "2031-12-22");
	createChildElement(pDetailsEle, "ttl", "5100");

	return payload;
}



int main(int argc, char *argv[])
{
	Environment::initialize("hello.log", AXIS2_LOG_LEVEL_TRACE);
	
	string end_point = "http://localhost:9091/axis2/services/ima/confirmUserRegistration";
	ServiceClient sc(end_point);

	Options* op = sc.getOptions();
	op->setHTTPMethod(AXIS2_HTTP_POST);
	op->setEnableREST(true);

//	OMNamespace * ns = new OMNamespace("http://ws.apache.org/axis2/services/echo", "ns1");
//	OMElement * payload = new OMElement(NULL, "registeruser");
//	OMElement * child = new OMElement(payload,"text", NULL);
 //	child->setText("Hello World!");

//	payload->setText("Hello World!");
//	OMElement* payload = assembleUserRegistrationPkt();
//OMElement* payload = assembleUserRegistrationConfirmationPkt();

	OMElement* payload = assembleUserLoginPkt();
//	OMElement* payload = assembleUserLogoutPkt();
//OMElement* payload = assembleAddInfoPkt();
	cout << endl << "Request: " << payload << endl;

	try
	{
		   OMElement* response = sc.request(payload, "");
		   //OMElement* response = sc.request(payload,  "http://ws.apache.org/axis2/c/samples/echo/soap_action");
			if (response)
			{
				cout << endl << "Response: " << response << endl;
			}
		}
		catch (WSFault & e)
		{
			if (sc.getLastSOAPFault())
			{
				cout << endl << "Fault: " << sc.getLastSOAPFault() << endl;
			}
			else
			{
 			cout << endl << "Error: " << e << endl;
			}
		}
		delete payload;
	}
 


